/**
 *
 * @param {Backbone.Events} dispatcher
 * @returns {MainVm}
 */
function MainVm(dispatcher) {
    this.dispatcher = dispatcher;
    this.Sites = ko.observableArray([]);
    this.currentSite = ko.observable(null);
    this.pageContainer = null;
    this.title = ko.observable('Агрегатор');
    var self = this;

    this.dispatcher.on("sites_loaded", this.onSitesLoaded, this);

    this.dispatcher.on("sections_loaded", this.onSectionsLoaded, this);

    this.chooseSite = function(Site) {
        self.currentSite(Site);
        self.dispatcher.trigger("site_choosen");
        $.mobile.changePage('#urls');
    };

}

MainVm.prototype = {

    onSitesLoaded: function(sites) {
        this.Sites($.map(sites, function(site) {
            return new SiteVm(site, this.dispatcher);
        }), this);

       this.dispatcher.trigger("need_sections_reload");
    },

    onSectionsLoaded: function(sections) {
        ko.utils.arrayForEach(this.Sites(), function(Site) {
            Site.setSections(sections);
        });
    }
};

